package com.lightboat.web.controller.crm;

import com.lightboat.common.annotation.Log;
import com.lightboat.common.core.controller.BaseController;
import com.lightboat.common.core.domain.AjaxResult;
import com.lightboat.common.core.page.TableDataInfo;
import com.lightboat.common.enums.BusinessType;
import com.lightboat.common.utils.PageUtils;
import com.lightboat.common.utils.poi.ExcelUtil;
import com.lightboat.system.domain.CrmLogisticsAddress;
import com.lightboat.system.service.ICrmLogisticsAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物流地址配置Controller
 *
 * @author lightboat
 * @date 2025-08-21
 */
@Tag(name = "物流地址配置")
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/logistics/address")
public class CrmLogisticsAddressController extends BaseController {

    private final ICrmLogisticsAddressService crmLogisticsAddressService;

    /**
     * 查询物流地址配置列表
     */
    @Operation(summary = "查询物流地址配置列表")
    @PreAuthorize("@ss.hasPermi('system:logistics:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmLogisticsAddress crmLogisticsAddress) {
        PageUtils.<CrmLogisticsAddress>chainOrderBy().orderBy(CrmLogisticsAddress::getId, true).apply();
        startPage();
        List<CrmLogisticsAddress> list = crmLogisticsAddressService.selectCrmLogisticsAddressList(crmLogisticsAddress);
        clearPage();
        return getDataTable(list);
    }

    /**
     * 导出物流地址配置列表
     */
    @Operation(summary = "导出物流地址配置列表")
    @PreAuthorize("@ss.hasPermi('system:logistics:address:export')")
    @Log(title = "物流地址配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmLogisticsAddress crmLogisticsAddress) {
        List<CrmLogisticsAddress> list = crmLogisticsAddressService.selectCrmLogisticsAddressList(crmLogisticsAddress);
        ExcelUtil<CrmLogisticsAddress> util = new ExcelUtil<CrmLogisticsAddress>(CrmLogisticsAddress.class);
        util.exportExcel(response, list, "物流地址配置数据");
    }

    /**
     * 获取物流地址配置详细信息
     */
    @Operation(summary = "获取物流地址配置详细信息")
    @PreAuthorize("@ss.hasPermi('system:logistics:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(crmLogisticsAddressService.selectCrmLogisticsAddressById(id));
    }

    /**
     * 新增物流地址配置
     */
    @Operation(summary = "新增物流地址配置")
    @PreAuthorize("@ss.hasPermi('system:logistics:address:add')")
    @Log(title = "物流地址配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmLogisticsAddress crmLogisticsAddress) {
        return toAjax(crmLogisticsAddressService.insertCrmLogisticsAddress(crmLogisticsAddress));
    }

    /**
     * 修改物流地址配置
     */
    @Operation(summary = "修改物流地址配置")
    @PreAuthorize("@ss.hasPermi('system:logistics:address:edit')")
    @Log(title = "物流地址配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmLogisticsAddress crmLogisticsAddress) {
        return toAjax(crmLogisticsAddressService.updateCrmLogisticsAddress(crmLogisticsAddress));
    }

    /**
     * 删除物流地址配置
     */
    @Operation(summary = "删除物流地址配置")
    @PreAuthorize("@ss.hasPermi('system:logistics:address:remove')")
    @Log(title = "物流地址配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(crmLogisticsAddressService.deleteCrmLogisticsAddressByIds(ids));
    }
}
